package LeetCode1;

//将二叉树转换为字符串
public class Num606 {
    private StringBuilder sb = new StringBuilder();

    public String tree2str(TreeNode root) {
        if (root == null) {
            return "";
        }
        sb.append(root.val);
        if (root.left != null) {
            sb.append("(");
            tree2str(root.left);
            sb.append(")");
        } else {
            if (root.right != null) {
                sb.append("()");
            }
        }
        if (root.right != null) {
            sb.append("(");
            tree2str(root.right);
            sb.append(")");
        }
        return sb.toString();
    }
}
